#
# Makefile, Makefile.in
# Makefiles for the Protea Project
# Copyright (C) 2008 Bruce A. James
#
# This file is part of the Protea Project.
#
# The Protea Project is free software; you can redistribute it and/or modify it 
# under the terms of the GNU General Public License as published by the 
# Free Software Foundation, either version 3 of the License, or (at your 
# option) any later version.
#
# The Protea Project is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
# for more details.
# 
# You should have received a copy of the GNU General Public License along 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Please send correspondence to:
# 
# theproteaproject@gmail.com
#

# Directory variables
srcdir = @srcdir@
libdir = @libdir@


# Compilation variables
CXX = @CXX@
CXXFLAGS = @CXXFLAGS@ @PICFLAG@ -D__REENTRANT @INTERNAL_FLAG@ @RELEASE_BUILD@ -I$(XERCESCROOT)/include -I$(XALANCROOT)/src

DEFS = @DEFS@


# Linking variables
LIBS=@LIBS@
LDFLAGS=@LDFLAGS@ @LDSHFLAGS@ @PICFLAG@
LINK=$(CXX) $(LDFLAGS)
XML=-L $(XERCESCROOT)/lib/ -lxerces-c -L $(XALANCROOT)/lib/ -lxalan-c -lxalanMsg
LIBNAME=libprotea$(TOOLSBASE).@SHLIBEXT@
LIBMAJOR=3
LIBMINOR=1
LIBBUILD=0 
LIB = $(LIBNAME).$(LIBMAJOR).$(LIBMINOR).$(LIBBUILD)

#
# Installation variables
#
INSTALL = /@INSTALL@



.SUFFIXES: .cpp .o .d

#
# Directory definitions
#
# SDIRS is the libprotea source code
SDIRS := . Fields Buffer AppHandler Database OSDep Security xml addons
# AUXDIRS is the utilities and other auxiliaries that link with libprotea
AUXDIRS = Traffic UI Utils Database/Drivers



#
# Library requirements
#
REQCPP := $(foreach dir,$(SDIRS),$(wildcard $(dir)/*.cpp))
REQOBJ := $(patsubst %.cpp,%.o,$(REQCPP))
REQDEP := $(patsubst %.cpp,%.d,$(REQCPP))

LINKOBJ := $(foreach dir,$(REQOBJ),$(DIROBJ)/$(dir))


all: Makefile $(LIB) SDK auxdirs

install: Makefile
	@echo .............................................
	@echo Installing the Protea Project Library and SDK
	@echo .............................................

	# Install the Protea Library
	@$(INSTALL) -d $(XSIROOT)/Lib
	ln -sf $(libdir)/$(LIB) $(XSIROOT)/Lib/$(LIB)
	ln -sf $(libdir)/$(LIB) $(XSIROOT)/Lib/$(LIBNAME).$(LIBMAJOR)
	ln -sf $(libdir)/$(LIB) $(XSIROOT)/Lib/$(LIBNAME)

	# Install the Protea SDK
	@$(INSTALL) -d $(XSIROOT)/SDK
	@$(INSTALL) SDK/*.h $(XSIROOT)/SDK

	# Create Appdic directory
	@$(INSTALL) -d $(XSIROOT)/Tools
	@$(INSTALL) -d $(XSIROOT)/Tools/appdic
	ln -sf $(libdir)/$(LIB) $(XSIROOT)/Tools/$(LIB)
	ln -sf $(libdir)/$(LIB) $(XSIROOT)/Tools/$(LIBNAME).$(LIBMAJOR)
	ln -sf $(libdir)/$(LIB) $(XSIROOT)/Tools/$(LIBNAME)

	# Do subdirectories now
	@for dir in $(AUXDIRS); do set -e; $(MAKE) -C $$dir install; done

	# Place INSTALL directions and COPYING files
	@$(INSTALL) INSTALL $(XSIROOT)
	@$(INSTALL) COPYING $(XSIROOT)
	@$(INSTALL) COPYING $(XSIROOT)/Tools

	@echo ............................................
	@echo The Protea Project Installation is complete.
	@echo ............................................

setup: Makefile SDK Install
	@echo ........................................
	@echo Creating the Protea Project Installation
	@echo ........................................

	@echo Copy over the installation files
	@cp -f Install/install_* $(XSIROOT)

#
# Runtime
#
	@echo Create the tar file
	@cp -f Install/Customizer_Normal.INIT $(XSIROOT)/CUSTOMIZER/INIT
	@tar czf Protea_Install.tgz Install/*.orig Install/License.txt -C $(XSIROOT) -X Install/SetupExclude.lst -X Install/RuntimeSetupExclude.lst -T Install/RuntimeSetupInclude.lst

	@echo Create the Runtime installation file
	@cat Install/install_header Protea_Install.tgz > Protea_Install_Runtime.sh

	@echo Change permissions
	@chmod +x Protea_Install_Runtime.sh

#
# SDK
#
	@echo Create the tar file
	@cp -f Install/Customizer_Pro.INIT $(XSIROOT)/CUSTOMIZER/INIT
	@tar czf Protea_Install.tgz Install/*.orig Install/License.txt -C $(XSIROOT) -X Install/SetupExclude.lst -X Install/SDKSetupExclude.lst -T Install/SDKSetupInclude.lst

	@echo Create the SDK installation file
	@cat Install/install_header Protea_Install.tgz > Protea_Install_SDK.sh

	@echo Remove temp files
	@rm -f Protea_Install.tgz $(XSIROOT)/install_header

	@echo Change permissions
	@chmod +x Protea_Install_SDK.sh

$(LIB): $(REQDEP) $(REQOBJ)
	@echo Linking shared library $@
	@$(LINK) @LDSHFLAGS@ $(REQOBJ) $(LIBS) $(XML) -o $@
	@$(INSTALL) $(LIB) $(libdir)
	ln -sf $(libdir)/$(LIB) $(libdir)/$(LIBNAME).$(LIBMAJOR)
	ln -sf $(libdir)/$(LIB) $(libdir)/$(LIBNAME)

SDK: $(LIB)
	@set -e; $(MAKE) -C SDK
	@echo "..."
	@echo "  SDK compiled successfully"
	@echo "..."
	@ln -sf $(libdir)/$(LIB) $(LIB)
	@ln -sf $(libdir)/$(LIB) $(LIBNAME).$(LIBMAJOR)
	@ln -sf $(libdir)/$(LIB) $(LIBNAME)

auxdirs: $(LIB) SDK
	@echo "..."
	@echo "Compiling auxiliary programs"
	@echo "..."
	@for dir in $(AUXDIRS); do set -e; $(MAKE) -C $$dir; done

require:
	@echo "Required CPP files: $(REQCPP)"
	@echo "Required DEP files: $(REQDEP)"

%.d: %.cpp
	@echo Compiling dependency: $*.d
	@set -e; g++ -MM -I$(XERCESCROOT)/include -I$(XALANCROOT)/src $< \
	| sed 's|$*.o:|$*.o $*.d:|g' > $@; \
	[ -s $@ ] || rm -f $@

%.o: %.cpp %.d
	@if (: >> $@ || : > $@) >/dev/null 2>&1; then rm -f $@; else \
	 dir=`echo $@ | sed 's,/[^/]*$$,,;s,^$$,.,'` $(MAKEDIR); fi
	@echo "---  Compiling source file: $*.cpp  ---"
	@set -e; $(CXX) $(CXXFLAGS) $(DEFS) -c $< -o $@

clean:
	@-rm $(REQOBJ) $(REQDEP)
#	@set -e; $(MAKE) -C SDK clean

allclean: clean
	@for dir in $(AUXDIRS); do set -e; $(MAKE) -C $$dir clean; done
	@set -e; $(MAKE) -C SDK clean
	@echo "Clean finished"

depend: $(REQDEP)

-include $(REQDEP)
